package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQuery;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo){
        qo.setStatus(Customer.STATUS_COMMON);
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        //通过shiro角色判断当前用户是否为管理员或者经理，如果是，给予查看潜在客户的权力
        if(!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            //通过主体获取员工信息
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        //销售人员的查询下拉框,只查询有Market角色或者Market_Manager角色的员工
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees",employees);

        //通过字典明细查询所有客户的职业信息
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByJob("job");
        model.addAttribute("jobs",jobs);

        //通过字典明细查询所有的客户来源信息
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySource("source");
        model.addAttribute("sources",sources);

        //通过字典明细查询所有与客户交流方式
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByCM("communicationMethod");
        model.addAttribute("ccts",ccts);

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
        public JsonResult saveOrUpdate(Customer customer){
            if (customer.getId() != null) {
                customerService.update(customer);
            }else {
                customerService.save(customer);
            }
            return new JsonResult();
    }
}
